//
//  YFCreatTemplateVc.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/30.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFCreatTemplateVc.h"
#import "YFThreeDTemplate.h"

@interface YFCreatTemplateVc ()
@property (weak, nonatomic) IBOutlet UILabel *systemCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *customCountLabel;
@property (weak, nonatomic) IBOutlet UIButton *systemButton;
@property (weak, nonatomic) IBOutlet UIButton *customButton;

@end

@implementation YFCreatTemplateVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self updateCountLabel];
}

- (void)updateCountLabel {
    self.title = @"Create Model";
    self.systemCountLabel.text = @([YFThreeDTemplate getCurrentUserAllSystemTemplateModels].count).stringValue;
    self.customCountLabel.text = @([YFThreeDTemplate getCurrentUserAllCustomTemplateModels].count).stringValue;
}


- (IBAction)creatSystemBtnClick:(UIButton *)sender {
    if (sender.isSelected) {
        // 结束
        [self endCreatTemplate];
    }else {
        [self creatTemplateWithType:TemplateMainTypeSystem];
    }
}

- (IBAction)creatCustomBtnClick:(UIButton *)sender {
    if (sender.isSelected) {
        // 结束
        [self endCreatTemplate];
    }else {
        [self creatTemplateWithType:TemplateMainTypeMy];
    }
}

- (void)creatTemplateWithType:(TemplateMainType)type {
    if (![YFCommunicationManager shareInstance].isPeripheralConnected) {
        FUIAlertView *alert = [FUIAlertView alertWithTitle:@"设备没有连接" message:nil cancelTitle:@"确定" confirmTitle:nil];
        [alert show];
        return;
    }
    if (type == TemplateMainTypeSystem) {
        if ([YFThreeDTemplate getCurrentUserAllSystemTemplateModels].count >= 8) {
           FUIAlertView *alert = [FUIAlertView alertWithTitle:@"系统模板已经有5条" message:nil cancelTitle:@"确定" confirmTitle:nil];
            [alert show];
            return;
        }
    }else if (type == TemplateMainTypeMy){
     
    }else{
        return;
    }
    NSArray *imageNames = @[@"吊球",@"挑球",@"长球",@"侧墙球",@"其他"];
    __weak typeof(self) weakSelf = self;
    [[YFCommunicationManager shareInstance] enter3DModel:^(BOOL success, NSError *error) {
        if (type == TemplateMainTypeSystem) {
            weakSelf.systemButton.selected = YES;
            weakSelf.customButton.enabled = NO;
        }else if (type == TemplateMainTypeMy){
            weakSelf.customButton.selected = YES;
            weakSelf.systemButton.enabled = NO;
        }
        
    } swing3DDataCallBlock:^(YFThreeDItem *threeDitem, NSError *error) {
        if (threeDitem) {
            NSString *title,*imageName;
            NSInteger index;
            if (type == TemplateMainTypeSystem) {
                index = (weakSelf.systemCountLabel.text.integerValue + 1);
                title = [NSString stringWithFormat:@"System template %ld",(long)index];
                imageName = imageNames[index%5];
            }else if (type == TemplateMainTypeMy){
                index = self.customCountLabel.text.integerValue + 1;
                title = [NSString stringWithFormat:@"Custom template %ld",(long)index];
                imageName = imageNames[index%5];
            }
            YFThreeDTemplate *template = [[YFThreeDTemplate alloc] initWithThreeDitem:threeDitem name:title templateImage:imageName mainType:type];
            [YFThreeDTemplate saveTemplateToDocument:template];
            [weakSelf updateCountLabel];
        }
    }];
}

- (void)endCreatTemplate {
    [[YFCommunicationManager shareInstance] quitRealTimeModelCompleted:^(BOOL success) {
        self.systemButton.selected = NO;
        self.systemButton.enabled = YES;
        self.customButton.selected = NO;
        self.customButton.enabled = YES;
    }];
}


@end
